package com.sgzn.sgpay.service.tcp;

import com.sgzn.sgpay.enums.AppName;
import com.sgzn.sgpay.enums.EnumDeviceState;
import com.sgzn.sgpay.model.request.SgsDeviceQueryRequest;
import com.sgzn.sgpay.model.request.SgsDeviceUpdateTcpRequest;
import com.sgzn.sgpay.model.response.SgsResultResponse;
import com.sgzn.sgpay.model.response.vo.DeviceInfoVo;
import com.sgzn.sgpay.util.SerialNoUtil;
import lombok.extern.slf4j.Slf4j;

import java.util.Date;
import java.util.Iterator;
import java.util.Map;
import java.util.TimerTask;

/**
 * Created by xxjs-gd-llf
 * DATETIME:2018/4/22 9:06
 * Description:
 */
@Slf4j
public class SgTcpMonitorHeartBeartCountTask extends TimerTask {
    @Override
    public void run() {
//        log.info("Check DeviceId Statue Every 3 Seconds");
        Iterator<Map.Entry<String,Integer>>  iterator = SgTcpServer.heartBeatCountMap.entrySet().iterator();
        while (iterator.hasNext()){
            Map.Entry entry = iterator.next();
            String deviceId =(String)entry.getKey();
            Integer integer = (Integer)entry.getValue();
            integer = integer + 1;

            if( SgTcpServer.heartBeatCountMap.get(deviceId) >= SgTcpServer.HEARTBEAT_TIMEOUT ){

                //超时,查询是否为Online,如果是更新为OffLine
                SgsDeviceQueryRequest sgsDeviceQueryRequest = new SgsDeviceQueryRequest();
                sgsDeviceQueryRequest.setInitiationID(SerialNoUtil.generateSerialNo(AppName.SGT));
                sgsDeviceQueryRequest.setReqTime(new Date());
                sgsDeviceQueryRequest.setLoginChannel(AppName.SGT.getNumber());
                sgsDeviceQueryRequest.setInitiationID( SerialNoUtil.generateSerialNo(AppName.SGT));
                sgsDeviceQueryRequest.setDeviceId(deviceId);
                SgsResultResponse<DeviceInfoVo> sgsResultResponse = SgTcpServer.sgsDeviceService.queryDeviceInfo(sgsDeviceQueryRequest);
                if(sgsResultResponse.getResult() != null ){
                    if(!sgsResultResponse.getResult().getState().equals(EnumDeviceState.OFFLINE.getStateCode())){
                        SgsDeviceUpdateTcpRequest sgsDeviceRequest = new SgsDeviceUpdateTcpRequest();
                        sgsDeviceRequest.setReqTime(new Date());
                        sgsDeviceRequest.setLoginChannel(AppName.SGT.getNumber());
                        sgsDeviceRequest.setInitiationID( SerialNoUtil.generateSerialNo(AppName.SGT));
                        sgsDeviceRequest.setDeviceId(deviceId);
                        sgsDeviceRequest.setState(EnumDeviceState.OFFLINE.getStateCode());
                        SgTcpServer.sgsDeviceService.updateDeviceTcp(sgsDeviceRequest);
                    }
                }
                //更新前先查下
                log.info("设备{} 状态为超时离线",deviceId);
                SgTcpServer.socketList.remove(deviceId);
                iterator.remove();

            }{
                SgTcpServer.heartBeatCountMap.put(deviceId,integer);
//                log.info("设备{}空闲超过{}秒,超时时间为{}秒",deviceId,SgTcpServer.HEARTBEATCOUNT_INTERVAL *integer ,SgTcpServer.HEARTBEATCOUNT_INTERVAL*SgTcpServer.HEARTBEAT_TIMEOUT);
            }

        }
    }
}
